<template>
	<view style="height:100vh">
		<layout>
			<view class="page-container">
				<!-- 顶部搜索栏 -->
				<view class="search-bar">
					<input class="search-input" placeholder="请输入模型名称或编号" v-model="searchKeyword"
						@confirm="handleSearch" />
					<view class="button-group">
						<button class="reset-btn" @click="handleReset">重置</button>
						<button class="search-btn" @click="handleSearch">搜索</button>
					</view>
				</view>

				<!-- 内容区域 -->
				<view class="content-wrapper">
					<!-- 新增按钮 -->
					<button class="add-btn" @click="showModal(null)">
						<text class="icon-add">+</text>
						新增模型
					</button>

					<!-- 卡片列表 -->
					<view class="card-list">
						<a-card v-for="(card, index) in filteredCards" :key="card.id" class="custom-card"
							:title="`编号：${card.number}`">
							<template #extra>
								<a-space>
									<button class="action-btn edit" @click="showModal(card.id)">修改</button>
									<button class="action-btn delete" @click="handleDelete(card.id)">删除</button>
								</a-space>
							</template>

							<view class="card-content">
								<view>模型名称：{{ card.modelName }}</view>
								<view>产品：{{ card.product }}</view>
								<view>型号：{{ card.modelNumber }}</view>
								<view>规格：{{ card.specification }}</view>
								<view>单位：{{ card.unit }}</view>
							</view>
						</a-card>
					</view>
				</view>

				<!-- 弹窗表单 -->
				<a-modal v-model:visible="visible" :title="currentId === null ? '新增模型' : '修改模型'" @ok="handleSubmit"
					@cancel="visible = false">
					<a-form :model="formState" layout="vertical">
						<a-form-item label="编号">
							<a-input v-model:value="formState.number" />
						</a-form-item>
						<a-form-item label="模型名称">
							<a-input v-model:value="formState.modelName" />
						</a-form-item>
						<a-form-item label="产品">
							<a-input v-model:value="formState.product" />
						</a-form-item>
						<a-form-item label="型号">
							<a-input v-model:value="formState.modelNumber" />
						</a-form-item>
						<a-form-item label="规格">
							<a-input v-model:value="formState.specification" />
						</a-form-item>
						<a-form-item label="单位">
							<a-input v-model:value="formState.unit" />
						</a-form-item>
					</a-form>
				</a-modal>
			</view>
		</layout>
	</view>

</template>

<script setup>
	import {
		ref,
		reactive
	} from 'vue';

	// 原始数据
	const originalCards = ref([{
		id: 1,
		number: '001',
		modelName: '模型A',
		product: '产品A',
		modelNumber: 'A-100',
		specification: '标准版',
		unit: '台'
	}]);

	// 响应式数据
	const searchKeyword = ref('');
	const filteredCards = ref([...originalCards.value]);
	const visible = ref(false);
	const currentId = ref(null);
	const formState = reactive({
		number: '',
		modelName: '',
		product: '',
		modelNumber: '',
		specification: '',
		unit: ''
	});

	// 搜索处理
	const handleSearch = () => {
		const keyword = searchKeyword.value.trim().toLowerCase();
		filteredCards.value = originalCards.value.filter(item =>
			item.modelName.toLowerCase().includes(keyword) ||
			item.number.toLowerCase().includes(keyword)
		);
	};

	// 重置处理
	const handleReset = () => {
		searchKeyword.value = '';
		filteredCards.value = [...originalCards.value];
	};

	// 显示弹窗
	const showModal = (id) => {
		currentId.value = id;
		if (id) {
			const target = originalCards.value.find(item => item.id === id);
			Object.assign(formState, target);
		} else {
			Object.keys(formState).forEach(key => formState[key] = '');
		}
		visible.value = true;
	};

	// 提交表单
	const handleSubmit = () => {
		if (currentId.value) {
			// 更新现有条目
			const index = originalCards.value.findIndex(item => item.id === currentId.value);
			originalCards.value[index] = {
				...formState,
				id: currentId.value
			};
		} else {
			// 新增条目
			originalCards.value.push({
				id: Date.now(),
				...formState
			});
		}
		handleSearch();
		visible.value = false;
	};

	// 删除条目
	const handleDelete = (id) => {
		const index = originalCards.value.findIndex(item => item.id === id);
		originalCards.value.splice(index, 1);
		handleSearch();
	};
</script>

<style scoped>
/* 碳中和主题色彩变量 */
:root {
	--primary-color: #27ae60;
	--primary-light: #2ecc71;
	--primary-dark: #219654;
	--secondary-color: #3498db;
	--accent-color: #16a085;
	--text-primary: #2c3e50;
	--text-secondary: #4a6572;
	--bg-gradient: linear-gradient(165deg, #f6fbf8, #e8f5f0);
	--card-gradient: linear-gradient(145deg, #ffffff, #f9fafb);
	--border-color: rgba(39, 174, 96, 0.1);
	--shadow-color: rgba(39, 174, 96, 0.08);
}

/* 页面容器 */
.page-container {
	min-height: 100vh;
	padding: 24rpx;
	background: var(--bg-gradient);
}

/* 搜索栏 */
.search-bar {
	padding: 24rpx;
	margin-bottom: 30rpx;
	background: var(--card-gradient);
	border-radius: 20rpx;
	display: flex;
	gap: 20rpx;
	box-shadow: 0 8rpx 20rpx var(--shadow-color);
	position: relative;
	z-index: 10;
	animation: fadeIn 0.5s ease forwards;
}

.search-input {
	flex: 1;
	height: 80rpx;
	padding: 0 24rpx;
	background-color: #f6fbf8;
	border: 1px solid var(--border-color);
	border-radius: 10rpx;
	font-size: 28rpx;
	color: var(--text-primary);
	transition: all 0.3s ease;
}

.search-input:focus {
	border-color: var(--primary-color);
	box-shadow: 0 0 0 2px rgba(39, 174, 96, 0.1);
}

.button-group {
	display: flex;
	gap: 16rpx;
}

.reset-btn, .search-btn {
	height: 80rpx;
	border-radius: 10rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 0 30rpx;
	font-size: 28rpx;
	font-weight: 500;
	transition: all 0.3s ease;
}

.reset-btn {
	background-color: #f9fafb;
	color: var(--text-secondary);
	border: 1px solid var(--border-color);
}

.search-btn {
	background: linear-gradient(145deg, var(--primary-color), var(--primary-light));
	color: white;
	border: none;
	box-shadow: 0 4rpx 8rpx rgba(39, 174, 96, 0.2);
}

.reset-btn:active {
	background-color: #e8f5f0;
}

.search-btn:active {
	background: linear-gradient(145deg, var(--primary-dark), var(--primary-color));
	transform: translateY(2rpx);
}

/* 内容区域 */
.content-wrapper {
	padding: 10rpx 0;
}

/* 新增按钮 */
.add-btn {
	background: linear-gradient(145deg, var(--secondary-color), #2980b9);
	color: white;
	height: 80rpx;
	border-radius: 50rpx;
	font-size: 28rpx;
	font-weight: 500;
	display: flex;
	align-items: center;
	justify-content: center;
	box-shadow: 0 4rpx 8rpx rgba(52, 152, 219, 0.2);
	transition: all 0.3s ease;
	margin-bottom: 30rpx;
	border: none;
	width: auto;
	align-self: flex-end;
	padding: 0 30rpx;
}

.add-btn:active {
	background: linear-gradient(145deg, #2980b9, #2472a4);
	transform: translateY(2rpx);
}

.icon-add {
	font-size: 28rpx;
	margin-right: 10rpx;
}

/* 卡片列表 */
.card-list {
	display: grid;
	grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
	gap: 24rpx;
}

:deep(.custom-card) {
	border-radius: 20rpx !important;
	overflow: hidden;
	background: var(--card-gradient) !important;
	border: 1px solid var(--border-color) !important;
	box-shadow: 0 8rpx 20rpx var(--shadow-color);
	transition: all 0.3s ease;
	animation: slideUp 0.5s ease forwards;
	opacity: 0;
	transform: translateY(20rpx);
}

:deep(.custom-card:nth-child(1)) { animation-delay: 0.05s; }
:deep(.custom-card:nth-child(2)) { animation-delay: 0.1s; }
:deep(.custom-card:nth-child(3)) { animation-delay: 0.15s; }
:deep(.custom-card:nth-child(4)) { animation-delay: 0.2s; }
:deep(.custom-card:nth-child(5)) { animation-delay: 0.25s; }

:deep(.custom-card:hover) {
	transform: translateY(-6rpx);
	box-shadow: 0 12rpx 30rpx rgba(39, 174, 96, 0.12);
}

:deep(.ant-card-head) {
	border-bottom: 1px solid var(--border-color) !important;
	background: linear-gradient(90deg, rgba(39, 174, 96, 0.05), rgba(46, 204, 113, 0.02));
	padding: 0 24rpx !important;
}

:deep(.ant-card-head-title) {
	font-size: 32rpx;
	font-weight: 600;
	color: var(--text-primary);
	position: relative;
	padding-left: 20rpx !important;
}

:deep(.ant-card-head-title::before) {
	content: '';
	position: absolute;
	left: 0;
	top: 50%;
	transform: translateY(-50%);
	width: 6rpx;
	height: 28rpx;
	background-color: var(--primary-color);
	border-radius: 3rpx;
}

:deep(.ant-card-extra) {
	display: flex;
	gap: 16rpx;
}

.action-btn {
	padding: 8rpx 16rpx;
	border-radius: 8rpx;
	font-size: 26rpx;
	transition: all 0.3s ease;
	border: none;
}

.edit {
	background-color: rgba(39, 174, 96, 0.1);
	color: var(--primary-color);
}

.delete {
	background-color: rgba(231, 76, 60, 0.1);
	color: #e74c3c;
}

.action-btn:active {
	transform: scale(0.95);
}

.edit:active {
	background-color: rgba(39, 174, 96, 0.2);
}

.delete:active {
	background-color: rgba(231, 76, 60, 0.2);
}

:deep(.ant-card-body) {
	padding: 24rpx !important;
}

.card-content {
	line-height: 1.8;
	font-size: 28rpx;
	color: var(--text-secondary);
}

.card-content view {
	position: relative;
	padding-left: 16rpx;
	margin-bottom: 12rpx;
}

.card-content view::before {
	content: '';
	position: absolute;
	left: 0;
	top: 50%;
	transform: translateY(-50%);
	width: 4rpx;
	height: 20rpx;
	background-color: var(--primary-light);
	border-radius: 2rpx;
	opacity: 0.7;
}

/* 表单样式 */
:deep(.ant-modal-content) {
	border-radius: 20rpx;
	overflow: hidden;
}

:deep(.ant-modal-header) {
	background: linear-gradient(90deg, rgba(39, 174, 96, 0.05), rgba(46, 204, 113, 0.02));
	border-bottom: 1px solid var(--border-color);
}

:deep(.ant-modal-title) {
	color: var(--text-primary);
	font-weight: 600;
}

:deep(.ant-form-item-label > label) {
	color: var(--text-secondary);
}

:deep(.ant-input) {
	border: 1px solid var(--border-color);
	border-radius: 10rpx;
	transition: all 0.3s ease;
}

:deep(.ant-input:focus) {
	border-color: var(--primary-color);
	box-shadow: 0 0 0 2px rgba(39, 174, 96, 0.1);
}

:deep(.ant-btn-primary) {
	background-color: var(--primary-color);
	border-color: var(--primary-color);
}

:deep(.ant-btn-primary:hover) {
	background-color: var(--primary-dark);
	border-color: var(--primary-dark);
}

/* 动画 */
@keyframes fadeIn {
	from { opacity: 0; }
	to { opacity: 1; }
}

@keyframes slideUp {
	from { opacity: 0; transform: translateY(20rpx); }
	to { opacity: 1; transform: translateY(0); }
}

/* 响应式设计 */
@media (max-width: 768px) {
	.search-bar {
		flex-direction: column;
	}
	
	.card-list {
		grid-template-columns: 1fr;
	}
}
</style>